package com.mgm.controller;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.mgm.dto.FileInfoDto;
import com.mgm.dto.ImageInfoDto;
import com.mgm.dto.VideoInfoDto;
import com.mgm.result.IMessage;
import com.mgm.result.IResult;
import com.mgm.utils.AwsClient;


@Controller
@RequestMapping("/file")
public class FileUploadController extends BaseController {
	
	private static Logger logger =LoggerFactory.getLogger(FileUploadController.class);
	
	/**
	 * 上传单个文件操作
	 * @param RequestParam 括号中的参数名file和表单的input节点的name属性值一致
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST)
	public ResponseEntity<IResult<FileInfoDto>> doUploadFile(@RequestParam("file") MultipartFile file){
		FileInfoDto fdto = null;
		if(!file.isEmpty()){
			logger.info("【文件上传】The file's contentType is {}, name is {}, size equal to {} and originalFilename is {}", file.getContentType(),
					file.getName(), file.getSize(), file.getOriginalFilename());
			try {
				String url = AwsClient.uploadToS3V2(convertFile(file)); //上传文件
				if (file.getContentType().contains("image")) { // 图片
					BufferedImage image = ImageIO.read(file.getInputStream());
					ImageInfoDto imagedto = new ImageInfoDto();
					imagedto.setMime(file.getContentType());
					imagedto.setType("image");
					imagedto.setSize(new Long(file.getSize()).intValue());
					imagedto.setHeight(image.getHeight()); // 获取图片高度，单位px
					imagedto.setWidth(image.getWidth()); // 获取图片宽度，单位px
					imagedto.setUrl(url); //上传url
					fdto = imagedto;
				} else if (file.getContentType().contains("video")) { // 视频
					//To Do ...
					//后期版本更新视频文件上传....
					VideoInfoDto videodto = new VideoInfoDto();
					videodto.setMime(file.getContentType());
					videodto.setType("video");
					videodto.setSize(new Long(file.getSize()).intValue());
					videodto.setTime(1000);
					videodto.setUrl(url); //上传url
					fdto = videodto;
				}
			} catch (IOException e) {
				logger.error("文件上传失败", e.getMessage());
				e.printStackTrace();
				IResult<FileInfoDto> res = IResult.error(IMessage.error("文件上传失败"));
				return new ResponseEntity<IResult<FileInfoDto>>(res, HttpStatus.OK);
				
			}
		}
		IResult<FileInfoDto> res = IResult.success(fdto);
		return new ResponseEntity<IResult<FileInfoDto>>(res, HttpStatus.OK);
	}

	/**
	 * text 表明文件是普通文本，理论上是可读的语言 text/plain, text/html, text/css, text/javascript
	 * image 表明是某种图像。不包括视频，但是动态图（比如动态gif）也使用image类型 image/gif, image/png,
	 * image/jpeg, image/bmp, image/webp audio 表明是某种音频文件 audio/midi, audio/mpeg,
	 * audio/webm, audio/ogg, audio/wav video 表明是某种视频文件 video/webm, video/ogg
	 * application 表明是某种二进制数据 application/octet-stream, application/pkcs12, application/vnd.mspowerpoint, application/xhtml+xml, application/xml, application/pdf, application/json
	 */

}
